from typing import List

"""
80. 删除有序数组中的重复项 II
https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150
"""


class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        i, j, time = 0, 0, 0
        while j < len(nums)-1 :
            j += 1
            if nums[i] == nums[j]or time>=2:
                time += 1
                if time <= 2:
                    i += 1
                    nums[i] = nums[j]
                else:
                    while j < len(nums)-1  and nums[i] == nums[j]:
                        j += 1
                    if nums[i] != nums[j]:
                        nums[i] = nums[j]
                        time = 0
            else:
                i += 1
                nums[i] = nums[j]
                time = 0
        return i + 1
        """
        官方题解
        """
        # n = len(nums)
        # if (n <= 2):
        #     return n
        # slow, fast = 2, 2
        # while (fast < n):
        #     if (nums[slow - 2] != nums[fast]):
        #         nums[slow] = nums[fast]
        #         slow += 1
        #     fast += 1
        # return slow


if __name__ == '__main__':
    nums = [1,1,1,2,2,3]
    print(Solution().removeDuplicates(nums))
    print(nums)
